<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of lianxiang
 *
 * @author sunday add 2014.5.28   | modify by dragon.hu @2015.12.3
 */
require_once 'BaseChannel.php';

//联想支付
class Lianxiang implements BaseChannel {
	
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		//$paymentKey="RkZBNURGNkJFNUNFRTEzNkQyOERBN0JCNTQxNkZGMkEyM0RGOEI1Q01USXhNelkwT0RJek5qTXdORGcxTkRJNE5UY3JNak16TlRBeU5UTXhNRGM0TlRRNE5EYzVOVEUyTmpnMk5qVXlNRFEwT1RjMk1EYzFOalkz"; // 12位支付密钥,当乐分配
		$paymentKey="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKsZAhqST7ckp3JuxnFWmvge1XOXjCfjger6LbrrG9uCCh86iqle4YaunoNOEWPpZ6p9+sRe4ydnCAC66/lfo2UbPZWAlrm/qKcRqnUP7x3RN2dgy3Lne/MfAjQQcJCamBkuSHBYFbv+Vq1BrvA74yizJJYz4rQvSBCZ36j486StAgMBAAECgYABxSJ2b+kPNfvdfTPkXEcCJh+DMKAskXf4nFn4wxHotXGxXQOuOOMQZ6IQLbCUXgfKhb8gZPLop3I3v7dNPyYLk1l5I3W799fhFc+6eJALDju3AkzqlcJyPDGl2oDqAqrgotg5LgAwQD1/YNb2Y6CRipDTMLuWdT73v979pDKdwQJBAPM1FYWLYXu2RV0JFWk5QOGaX7CIvZF5Fb5up0BUIfHO6qOZ6D189yrG8XwveZUF8F/2pjQEYV/fB51dIFw+5qkCQQC0GO3hy184rU+y0BZKYvB3QG5igc7BnCopfmM4yezt7O/32xO5a101EoVFvzAy9nRPQ+3mp4CPcDX6uFWRSARlAkEAkSqoU/PkFgGv2V5op0wDTCXm4+parJBHyEkoD5+oOzs0lr5w/vKuXZwt2+BChA7tcXo5rmbG8IChfO5vOGBbMQJAVY9OXCoBJ+z2Yw4GuPo+IXljt7rRnhtx0FqyCsWroPEipA7ZVQqTsmYFGLiWzm9VnF66RA0pv3tguacfR7D+HQJBAL5MmUaqklv1xKnj91Uf1wXayg0LQTukKhimDKIDQ70uSTFcOoEZ3HdKujKQuYMVQhKYEUNqc3wGze+nzKmPfk0=";
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) {
		//$paymentKey="RkZBNURGNkJFNUNFRTEzNkQyOERBN0JCNTQxNkZGMkEyM0RGOEI1Q01USXhNelkwT0RJek5qTXdORGcxTkRJNE5UY3JNak16TlRBeU5UTXhNRGM0TlRRNE5EYzVOVEUyTmpnMk5qVXlNRFEwT1RjMk1EYzFOalkz"; // 12位支付密钥,当乐分配
		$paymentKey="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKsZAhqST7ckp3JuxnFWmvge1XOXjCfjger6LbrrG9uCCh86iqle4YaunoNOEWPpZ6p9+sRe4ydnCAC66/lfo2UbPZWAlrm/qKcRqnUP7x3RN2dgy3Lne/MfAjQQcJCamBkuSHBYFbv+Vq1BrvA74yizJJYz4rQvSBCZ36j486StAgMBAAECgYABxSJ2b+kPNfvdfTPkXEcCJh+DMKAskXf4nFn4wxHotXGxXQOuOOMQZ6IQLbCUXgfKhb8gZPLop3I3v7dNPyYLk1l5I3W799fhFc+6eJALDju3AkzqlcJyPDGl2oDqAqrgotg5LgAwQD1/YNb2Y6CRipDTMLuWdT73v979pDKdwQJBAPM1FYWLYXu2RV0JFWk5QOGaX7CIvZF5Fb5up0BUIfHO6qOZ6D189yrG8XwveZUF8F/2pjQEYV/fB51dIFw+5qkCQQC0GO3hy184rU+y0BZKYvB3QG5igc7BnCopfmM4yezt7O/32xO5a101EoVFvzAy9nRPQ+3mp4CPcDX6uFWRSARlAkEAkSqoU/PkFgGv2V5op0wDTCXm4+parJBHyEkoD5+oOzs0lr5w/vKuXZwt2+BChA7tcXo5rmbG8IChfO5vOGBbMQJAVY9OXCoBJ+z2Yw4GuPo+IXljt7rRnhtx0FqyCsWroPEipA7ZVQqTsmYFGLiWzm9VnF66RA0pv3tguacfR7D+HQJBAL5MmUaqklv1xKnj91Uf1wXayg0LQTukKhimDKIDQ70uSTFcOoEZ3HdKujKQuYMVQhKYEUNqc3wGze+nzKmPfk0=";
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		//require_once('include/lianxiang/IappDecrypt.php');
		require_once ('include/lianxiang/lib/lenovo_cashier_rsa.inc.php');
		$orderLog = new ChargeOrder();
		$orderLog->log("lenovo", $_POST);
		/*
transdata={"exorderno":"0a978b24-fcdb-4b47-a117-28f02dd45136","transid":"04614070915125911980","waresid":1,"appid":"1406301138393.app.ln",
"feetype":0,"money":100,"result":0,"transtype":0,"transtime":"2014-07-09 15:14:34","count":1,"cpprivate":"0a978b24-fcdb-4b47-a117-28f02dd45136","paytype":401}
&sign=99bc7f778044b362900945679bafe2b2 3b96fc90725172fe5297ed6ac140e531 a056937d605d0f1530de202e8cbe50c0 
		 */
		//来自callback.php
		$transdata = str_replace("\\", "", trim($_POST['transdata']));
		$orderLog->log("lenovo", $transdata);
		//$transdata = trim($_POST['transdata']);
		$jsonData = json_decode($transdata, true);
		
		$sign =  trim($_POST['sign']); 
		
		//var_dump($_POST['transdata']);
		//var_dump(str_replace("\\", "", trim($_POST['transdata'])));
		//var_dump($jsonData);
		//var_dump($_POST['sign']);
		//var_dump(trim($_POST['sign']));
		//exit(0);
		
		if (empty($transdata)) {
			//echo '{"error":"3"}';
			$orderLog->log("lenovo", "transdata is empty!!!");
			return "FAILURE";
		}
		
		if (empty($sign)) {
			//echo '{"error":"3"}';
			$orderLog->log("lenovo", "sign is empty!!!");
			return "FAILURE";
		}
		
		//$sign_local = sign($transdata, $paymentKey);
		//var_dump($transdata);
		//var_dump($paymentKey);
		//var_dump($sign);
		
		$result = verify($transdata, $paymentKey, $sign);
		
		if($result)
		{
			$status = STATUS_SUCCESS;
		}else
		{
			$status = STATUS_FAILD;
		}
		//exit(0);	
		
		if(true == $orderLog->recordOrder($status, $jsonData['money']/100, $jsonData['exorderno'], "lianxiang", $platform,$jsonData['transid']))
		{
			$orderLog->log('lenovo','platform:'.$platform.'amount:'.$jsonData['money']/100 .' order_id:'.$jsonData['exorderno'].' success');
			$ret = "SUCCESS";
		}else
		{
			$orderLog->log('lenovo','platform:'.$platform.'amount:'.$jsonData['money']/100 .' order_id:'.$jsonData['exorderno'].' fail');
			echo " order:".$jsonData['exorderno']." insert database fail";
			//$orderLog->log(" order:".$json['order_id_com']." insert database fail");
			$ret = "FAILURE";
		}
		return $ret;
		/*
		//echo " get data ".rawurldecode($trans_data);
		//echo " get sign ".rawurldecode($sing);
		//$sign = "99bc7f778044b362900945679bafe2b2 3b96fc90725172fe5297ed6ac140e531 a056937d605d0f1530de202e8cbe50c0";
		//echo " trans_data:".$trans_data;
		//echo " sign:".$sign;
		//echo " paymentKey:".$paymentKey;
		$trans_data = stripslashes(stripslashes($trans_data));
		$result = $lx->validsign($trans_data,$sign,$paymentKey);
		//for test 
		//$notify_data='{"order_id_com":"2013050900000712","user_id":"10010","amount":"0.10","account":"test001","order_id":"2013050900000713","result":"success"}';
		//if(1==1)
		//var_dump($trans_data);
		$jsonData = json_decode($trans_data,true);
		//var_dump($jsonData);
		//echo "result=".$result;
		//验证签名
		if (0 == $result)	
		{
			//逻辑处理, $notify_data: json数据(格式: {"order_id_com":"2013050900000712","user_id":"10010",
			//				"amount":"0.10","account":"test001","order_id":"2013050900000713","result":"success"})
			//$json = json_decode($notify_data, true);

			//result为success才是支付成功
			if('0'==$jsonData['result'])
			{
				$status = STATUS_SUCCESS;
			}else 
			{
				$status = STATUS_FAILD;
			}
			
			if(true == $orderLog->recordOrder($status, $jsonData['money']/100, $jsonData['exorderno'], "lenovo", $platform,$jsonData['transid']))
			{
				$orderLog->log('lenovo','platform:'.$platform.'amount:'.$jsonData['money']/100 .' order_id:'.$jsonData['exorderno'].' success');
				$ret = "SUCCESS";
	        }else 
	        { 			
				$orderLog->log('lenovo','platform:'.$platform.'amount:'.$jsonData['money']/100 .' order_id:'.$jsonData['exorderno'].' fail');
				echo " order:".$jsonData['exorderno']." insert database fail";
				//$orderLog->log(" order:".$json['order_id_com']." insert database fail");
				$ret = "FAILED";
	        }
		} else 
		{
			//echo " order:".$json['order_id_com']." sign verify fail";
			$orderLog->log('lenovo'," order:".$jsonData['exorderno']." sign verify fail");
			echo " sign error ";
			$ret = "FAILED";
		}	
		return $ret;
		*/		
	}	
}